/*
	srgp_state_Win.c
	MS Windows version of state.c

*/

#include "srgplocal.h"
#include "srgp_win.h"
#include <string.h>
#include <dos.h>

char *szAppName = "SRGP";


HANDLE srgp__colorlist;
int	   srgp__nCmdShow;

int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
				   LPSTR lpszCmdLine, int nCmdShow)
{
	int			i, j, start;
	char		*argstr;
	WNDCLASS	wndClass;

	if (!hPrevInstance)
	{
		wndClass.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
		wndClass.lpfnWndProc    = WndProc;
		wndClass.cbClsExtra		= 0;
		wndClass.cbWndExtra		= 0;
		wndClass.hInstance		= hInstance;
		wndClass.hIcon			= LoadIcon (hInstance, "srgp");
		wndClass.hCursor		= NULL;
		wndClass.hbrBackground	= GetStockObject (WHITE_BRUSH);
		wndClass.lpszMenuName	= NULL;
		wndClass.lpszClassName  = szAppName;

		if (!RegisterClass(&wndClass))
			return FALSE;
	}

	srgp__hInstance = hInstance;
	srgp__nCmdShow = nCmdShow;

	//srgp__colorlist = LoadResource(hInstance,
	//				  FindResource(hInstance, "colorlist", "TEXT"));

	srgp__colorlist = "snow 255 250 250 ghostwhite 248 248 255 whitesmoke 245 245 245 gainsboro 220 220 220 floralwhite 255 250 240 oldlace 253 245 230 linen 250 240 230 antiquewhite 250 235 215 papayawhip 255 239 213 blanchedalmond 255 235 205 bisque 255 228 196 peachpuff 255 218 185 navajowhite 255 222 173 moccasin 255 228 181 cornsilk 255 248 220 ivory 255 255 240 lemonchiffon 255 250 205 seashell 255 245 238 honeydew 240 255 240 mintcream 245 255 250 azure 240 255 255 aliceblue 240 248 255 lavender 230 230 250 lavenderblush 255 240 245 mistyrose 255 228 225 white 255 255 255 black   0   0   0 darkslategray  47  79  79 darkslategrey  47  79  79 dimgray 105 105 105 dimgrey 105 105 105 slategray 112 128 144 slategrey 112 128 144 lightslategray 119 136 153 lightslategrey 119 136 153 gray 192 192 192 grey 192 192 192 lightgrey 211 211 211 lightgray 211 211 211 midnightblue  25  25 112 navy   0   0 128 navyblue   0   0 128 cornflowerblue 100 149 237 darkslateblue  72  61 139 slateblue 106  90 205 mediumslateblue 123 104 238 lightslateblue 132 112 255 mediumblue   0   0 205 royalblue  65 105 225 blue   0   0 255 dodgerblue  30 144 255 deepskyblue   0 191 255 skyblue 135 206 235 lightskyblue 135 206 250 steelblue  70 130 180 lightsteelblue 176 196 222 lightblue 173 216 230 powderblue 176 224 230 paleturquoise 175 238 238 darkturquoise   0 206 209 mediumturquoise  72 209 204 turquoise  64 224 208 cyan   0 255 255 lightcyan 224 255 255 cadetblue  95 158 160 mediumaquamarine 102 205 170 aquamarine 127 255 212 darkgreen   0 100   0 darkolivegreen  85 107  47 darkseagreen 143 188 143 seagreen  46 139  87 mediumseagreen  60 179 113 lightseagreen  32 178 170 palegreen 152 251 152 springgreen   0 255 127 lawngreen 124 252   0 green   0 255   0 chartreuse 127 255   0 mediumspringgreen   0 250 154 greenyellow 173 255  47 limegreen  50 205  50 yellowgreen 154 205  50 forestgreen  34 139  34 olivedrab 107 142  35 darkkhaki 189 183 107 khaki 240 230 140 palegoldenrod 238 232 170 lightgoldenrodyellow 250 250 210 lightyellow 255 255 224 yellow 255 255   0 gold 255 215   0 lightgoldenrod 238 221 130 goldenrod 218 165  32 darkgoldenrod 184 134  11 rosybrown 188 143 143 indianred 205  92  92 saddlebrown 139  69  19 sienna 160  82  45 peru 205 133  63 burlywood 222 184 135 beige 245 245 220 wheat 245 222 179 sandybrown 244 164  96 tan 210 180 140 chocolate 210 105  30 firebrick 178  34  34 brown 165  42  42 darksalmon 233 150 122 salmon 250 128 114 lightsalmon 255 160 122 orange 255 165   0 darkorange 255 140   0 coral 255 127  80 lightcoral 240 128 128 tomato 255  99  71 orangered 255  69   0 red 255   0   0 hotpink 255 105 180 deeppink 255  20 147 pink 255 192 203 lightpink 255 182 193 palevioletred 219 112 147 maroon 176  48  96 mediumvioletred 199  21 133 violetred 208  32 144 magenta 255   0 255 violet 238 130 238 plum 221 160 221 orchid 218 112 214 mediumorchid 186  85 211 darkorchid 153  50 204 darkviolet 148   0 211 blueviolet 138  43 226 purple 160  32 240 mediumpurple 147 112 219 thistle 216 191 216";

	SRGP__memInit();

	UserMain(0,0);

	SRGP__memFree();

	return 0;
}


void SRGP__initGraphicsDevice (char *name, int requested_planes,
							   boolean debugasap)
{
	HWND hWnd;
	int width, height;
	RECT 	clientRect;
	RECT    winRect;
	HDC		hdc;

	/* Create and display the window */
	hWnd = CreateWindow(szAppName, name,
						(WS_OVERLAPPED | WS_CAPTION | WS_MINIMIZEBOX |
						 WS_MAXIMIZEBOX | WS_THICKFRAME | WS_SYSMENU),
						 CW_USEDEFAULT, CW_USEDEFAULT,
						 srgp__curActiveCanvasSpec.max_xcoord + 1,
						 srgp__curActiveCanvasSpec.max_ycoord + 1,
						 NULL, NULL, srgp__hInstance, NULL);
	srgp__curActiveCanvasSpec.drawable.win = hWnd;
	srgp__canvasTable[0] = srgp__curActiveCanvasSpec;

	GetWindowRect(hWnd, &winRect);
	GetClientRect(hWnd, &clientRect);
	width = srgp__curActiveCanvasSpec.max_xcoord + 1 +
			(winRect.right - winRect.left) -
			(clientRect.right - clientRect.left);
	height = srgp__curActiveCanvasSpec.max_ycoord + 1 +
			 (winRect.bottom - winRect.top) -
			 (clientRect.bottom - clientRect.top);

	SetWindowPos(hWnd, NULL, 0, 0, width, height, SWP_NOMOVE);

	hdc = GetDC(hWnd);
	SelectObject (hdc, GetStockObject (NULL_BRUSH));
	SetBkMode (hdc, TRANSPARENT);
	SetTextAlign(hdc, TA_BASELINE);

	SRGP__initColor (requested_planes);

	ReleaseDC (hWnd, hdc);

	UpdateWindow (hWnd);
	ShowWindow (hWnd, srgp__nCmdShow);

	srgp__canvasTable[0] = srgp__curActiveCanvasSpec;
}

/** SRGP_refresh
**/
void SRGP_refresh()
{
/* only needed on X11 workstations */
}

/** SRGP_enableSynchronous
No-op for Windows version.
**/
void SRGP_enableSynchronous()
{
}

/** SRGP_allowResize
No-op for Windows version.
**/
void SRGP_allowResize(boolean allow)
{
}

void SRGP__forceScreenResize (int newwidth, int newheight)
{
	RECT 	clientRect;
	RECT    winRect;
	int width, height;

	GetWindowRect(srgp__canvasTable[0].drawable.win, &winRect);
	GetClientRect(srgp__canvasTable[0].drawable.win, &clientRect);
	width = newwidth +
			(winRect.right - winRect.left) -
			(clientRect.right - clientRect.left);
	height = newheight +
			 (winRect.bottom - winRect.top) -
			 (clientRect.bottom - clientRect.top);

	SetWindowPos(srgp__canvasTable[0].drawable.win, NULL, 0, 0,
				 width, height, SWP_NOMOVE);

}

int SRGP_inquireWinInstance(void)
{
	return srgp__hInstance;
}